package main

import (
	"fmt"
	"os"
	"sync"
	"time"
)

func main() {
	wg := sync.WaitGroup{}
	wg.Add(10)
	msg := make(chan string, 10)
	fd, _ := os.OpenFile("test.txt", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
	var i = 0
	go func() {
		for {
			msg <- "aaa\n"
			wg.Done()
			i++
			time.Sleep(time.Second)
		}
	}()

	go func() {
		for {
			select {
			case m := <-msg:
				fmt.Print(m)
				fd.Write([]byte(m))
				if i >= 5 {
					fd.Close()
					os.Rename("test.txt", "aaaaaaa.txt")
				}
			}
		}
	}()
	wg.Wait()

}
